package com.app.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.app.model.Article;
import com.app.mapper.ArticleMapper;
import com.app.page.CommonPage;
import com.app.request.ArticleRequest;
import com.app.request.PageParamRequest;
import com.app.response.ArticleResponse;
import com.app.service.IArticleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 文章管理 服务实现类
 * </p>
 *
 * @author ghoul
 * @since 2024-12-17
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {

    @Override
    public PageInfo<ArticleResponse> getAdminList(ArticleRequest request, PageParamRequest pageParamRequest) {
        Page<Article> articlePage = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<Article>();
        if (StrUtil.isNotBlank(request.getTitle())) {
            queryWrapper.like(Article::getTitle, request.getTitle());
        }
        if (request.getType() != null) {
            queryWrapper.eq(Article::getType, request.getType());
        }
        queryWrapper.eq(Article::getIsDel, false);
        queryWrapper.orderByDesc(Article::getId);
        List<Article> articleList = list(queryWrapper);
        List<ArticleResponse> responseList = new ArrayList<>();
        if (CollUtil.isEmpty(articleList)) {
            return CommonPage.copyPageInfo(articlePage, responseList);
        }
        for (Article article : articleList) {
            ArticleResponse response = new ArticleResponse();
            BeanUtils.copyProperties(article, response);
            if (article.getType() != null) {
                if (article.getType() == 1) {
                    response.setTypeText("巡察公告");
                }
                if (article.getType() == 2) {
                    response.setTypeText("巡察动态");
                }
                if (article.getType() == 3) {
                    response.setTypeText("反映须知");
                }
            }
            responseList.add(response);
        }
        return CommonPage.copyPageInfo(articlePage, responseList);
    }

    @Override
    public PageInfo<ArticleResponse> getSmallList(ArticleRequest request, PageParamRequest pageParamRequest) {
        Page<Article> articlePage = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<Article>();
        if (StrUtil.isNotBlank(request.getTitle())) {
            queryWrapper.like(Article::getTitle, request.getTitle());
        }
        queryWrapper.eq(Article::getType, request.getType());
        queryWrapper.eq(Article::getIsDel, false);
        queryWrapper.eq(Article::getStatus, false);
        queryWrapper.orderByDesc(Article::getId);
        List<Article> articleList = list(queryWrapper);
        List<ArticleResponse> responseList = new ArrayList<>();
        if (CollUtil.isEmpty(articleList)) {
            return CommonPage.copyPageInfo(articlePage, responseList);
        }
        for (Article article : articleList) {
            ArticleResponse response = new ArticleResponse();
            BeanUtils.copyProperties(article, response);
            responseList.add(response);
        }
        return CommonPage.copyPageInfo(articlePage, responseList);
    }


    @Override
    public boolean create(Article article) {
        if (ObjectUtil.isNotNull(article.getId())) {
            article.setUpdateTime(LocalDateTime.now());
            return updateById(article);
        }
        article.setCreateTime(LocalDateTime.now());
        return save(article);
    }

    @Override
    public boolean deleteById(Integer id) {
        Article article = getById(id);
        if (ObjectUtil.isNotNull(article)) {
            article.setIsDel(true);
            return updateById(article);
        }
        return false;
    }

    @Override
    public ArticleResponse getDetail(Integer id) {
        ArticleResponse articleResponse = new ArticleResponse();
        Article article = getById(id);
        article.setViewCount(article.getViewCount() + 1);
        updateById(article);
        BeanUtils.copyProperties(article, articleResponse);
        return articleResponse;
    }

    @Override
    public boolean updateStatus(Integer id, boolean status) {
        Article article = getById(id);
        if (ObjectUtil.isNotNull(article)) {
            article.setStatus(status);
            return updateById(article);
        }
        return false;
    }

    @Override
    public Article instructions() {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<Article>();
        queryWrapper.eq(Article::getIsDel, false);
        queryWrapper.eq(Article::getStatus, false);
        queryWrapper.eq(Article::getType, 3);
        queryWrapper.orderByDesc(Article::getId);
        List<Article> articleList = list(queryWrapper);
        if (articleList != null && !articleList.isEmpty()) {
            return articleList.get(0);
        }
        return null;
    }
}
